package com.chen.shopping_manager_api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.shopping_common.pojo.ProductType;
import com.chen.shopping_common.result.BaseResult;
import com.chen.shopping_common.service.ProductTypeService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productType")
public class ProductTypeController {

    @DubboReference
    private ProductTypeService productTypeService;

    @PostMapping("/add")
    public BaseResult addProductType(@RequestBody ProductType productType){
        productTypeService.addProductType(productType);
        return BaseResult.ok();
    }

    @PutMapping("/update")
    public BaseResult updateProductType(@RequestBody ProductType productType){
        productTypeService.updateProductType(productType);
        return BaseResult.ok();
    }

    @DeleteMapping("/delete")
    public BaseResult deleteProductType(Long id){
        productTypeService.deleteProductType(id);
        return BaseResult.ok();
    }

    @GetMapping("/findById")
    public BaseResult<ProductType> findProductTypeById(Long id){
        return BaseResult.ok(productTypeService.findProductTypeById(id));
    }

    @GetMapping("/search")
    public BaseResult<Page<ProductType>> findProductTypeByPage(ProductType productType,int page,int size){
        return BaseResult.ok(productTypeService.findProductTypeByPage(productType,page,size));
    }

    @GetMapping("/findProductType")
    public BaseResult<List<ProductType>> findProductTypeByCondition(ProductType productType){
        return BaseResult.ok(productTypeService.findProductTypeByCondition(productType));
    }

    //根据父级查询所有子级集合
    @GetMapping("/findByParentId")
    public BaseResult<List<ProductType>> findByParentId(Long parentId){
        ProductType productType = new ProductType();
        productType.setParentId(parentId);
        List<ProductType> productTypeByCondition = productTypeService.findProductTypeByCondition(productType);
        return BaseResult.ok(productTypeByCondition);
    }
}
